package com.leecode.xiehf.page_19;

import com.leecode.xiehf.page_16.Solution_0783;

import java.util.ArrayList;
import java.util.List;

public class Solution_0897 {

    public static void main(String[] args) {
        Solution_0897 solution = new Solution_0897();
        solution.increasingBST(
                new TreeNode(5,
                        new TreeNode(3,
                                new TreeNode(2,
                                        new TreeNode(1),
                                        null),
                                new TreeNode(4)
                        ),
                        new TreeNode(6, null,
                                new TreeNode(8,
                                        new TreeNode(7),
                                        new TreeNode(9)
                                )
                        )
                )
        );
    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public TreeNode increasingBST(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        bst(root, list);
        TreeNode parent = new TreeNode();
        TreeNode cur = parent;
        for(Integer num: list) {
            cur.right = new TreeNode(num);
            cur = cur.right;
        }
        return parent.right;
    }

    public void bst(TreeNode node, List<Integer> list) {
        if (node == null) {
            return;
        }
        bst(node.left, list);
        list.add(node.val);
        bst(node.right, list);
    }
}
